﻿using Catastrophe.BasePC.UI.DbDal;
using Catastrophe.BasePC.UI.Model;
using Catastrophe.BasePC.UI.UI;
using Catastrophe.BasePC.UI.Model;
using Catastrophe.Initialize.IOCProvier;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Catastrophe.BasePC.UI.UIMode
{
    /// <summary>
    /// 菜单编辑
    /// </summary>
    public partial class UcSysMenuEditModel : ObservableObject
    {
        /// <summary>
        /// 节点编号
        /// </summary>
        [ObservableProperty]
        private int index;
        /// <summary>
        /// 菜单名称
        /// </summary>
        [ObservableProperty]
        private string name;
        /// <summary>
        /// 是否显示
        /// </summary>
        [ObservableProperty]
        private bool visible;
        /// <summary>
        /// 是否固定
        /// </summary>
        [ObservableProperty]
        private bool is_fixed;
        /// <summary>
        /// 生命周期
        /// </summary>
        [ObservableProperty]
        private string instance_life;
        /// <summary>
        /// 程序集
        /// </summary>
        [ObservableProperty]
        private string assembly;
        /// <summary>
        /// 资源名称
        /// </summary>
        [ObservableProperty]
        private string resource_name;
        /// <summary>
        /// 站点
        /// </summary>
        [ObservableProperty]
        private string soft;

        /// <summary>
        /// 窗口名称
        /// </summary>
        [ObservableProperty]
        private string formName;
        /// <summary>
        /// 节点
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<NodeItem> nodeItems;
        /// <summary>
        /// 生命周期
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<string> instance_lifes;

        /// <summary>
        /// 选中的节点
        /// </summary>
        [ObservableProperty]
        private NodeItem selectnodeItems;
        /// <summary>
        /// 数据id
        /// </summary>
        public int dataId { get; set; }
        /// <summary>
        /// 数据访问
        /// </summary>
        private SysMenuDal _baseMenuNodeDal { get; set; }
        public ICommand OkCommand { get; set; }
        /// <summary>
        /// 确认触发事件
        /// </summary>
        public event Action<bool> OkClick;
        /// <summary>
        /// 
        /// </summary>
        public UcSysMenuEditModel()
        {
            _baseMenuNodeDal = new SysMenuDal();
            nodeItems = new ObservableCollection<NodeItem>();
            OkCommand = new RelayCommand(OkCommandMethod);
            instance_lifes = new ObservableCollection<string>();
            IniInstanceLife();
        }
        /// <summary>
        /// 初始化生命周期
        /// </summary>
        private void IniInstanceLife()
        {
            instance_lifes.Clear();
            var enumlist = Enum.GetNames<InstanceLifeEnum>();
            foreach (var item in enumlist)
            {
                instance_lifes.Add(item);
            }
            if (Instance_lifes.Count > 0)
            {
                Instance_life = Instance_lifes[0];
            }
        }
        private void OkCommandMethod()
        {
            SysMenuEntity newentity = new SysMenuEntity()
            {
                NodeName = Name,
                BaseIndex = Index,
                Assembly = Assembly,
                IsFixed = Is_fixed,
                Visible = Visible,
                InstanceLife = Instance_life,
                ResourceName = Resource_name,
                TimeStamp = DateTime.Now.ToString(),
                Id=dataId,
                SoftId=Soft
            };
            if (SelectnodeItems!=null)
            {
                newentity.ParentIndex= SelectnodeItems.index;
            }
            else
            {
                newentity.ParentIndex = -1;
            }
            if (dataId > 0)
            {
                _baseMenuNodeDal.UpdateData(newentity);
                OkClick?.Invoke(true);
            }
            else
            {
                var count= _baseMenuNodeDal.QueryDataByIndex(newentity.BaseIndex);
                if (count>0)
                {
                    MessageBox.Show("节点号重复！"); return;
                }
              
                _baseMenuNodeDal.AddData(newentity);
                OkClick?.Invoke(true);
            }
        }
    }
}
